﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCrawlerLib;

namespace WCrawler.Controls
{
    public partial class ucForumTemplates : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Panel pnlCommand = (Panel)ucForumControl.FindControl("pnlCommand");
                //pnlCommand.Visible = false;
                //GridView gridViewForums = (GridView)ucForumControl.FindControl("gridViewForums");
                //gridViewForums.SelectedIndex = 0;
                //CommandField cmdField = (CommandField)gridViewForums.Columns[0];
                //cmdField.ShowEditButton = false;
                //cmdField.ShowDeleteButton = false;
            }
        }

        public Forum GetForumSession(int forumId)
        {
            Forum forum;
            //Get obj _forum
            if (Session[forumId.ToString()] != null)
            {
                forum = (Forum)Session[forumId.ToString()];
            }
            else
            {
                forum = new Forum();
                forum = forum.GetForum(forumId);
                //forum.LoadForum();
                Session[forumId.ToString()] = forum;
            }
            return forum;
        }

        protected void TemplateDetailsLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            int id = -1;
            if (Session["ucForum_SelectedValue"] != null)
            {
                id = (int)Session["ucForum_SelectedValue"];
                Forum objForum = GetForumSession(id);
                TemplateDetail objTemplateDetails = new TemplateDetail();
                IQueryable<TemplateDetail> query;
                if (Session["TemplateDetailsId"] != null)
                    query = objTemplateDetails.GetTemplateDetailsByTemplateId(objForum.ForumTypeId.Value, int.Parse(Session["TemplateDetailsId"].ToString()));
                else
                    query = objTemplateDetails.GetRemainingTemplateDetailsOfForum(objForum.ForumId, objForum.ForumTypeId.Value);
                e.Result = query;
            }
        }

        protected void btnAddForumTemplate_Click(object sender, EventArgs e)
        {
            //GridView gridViewForums = (GridView)ucForumControl.FindControl("gridViewForums");
            //Forum forum = GetForumSession((int)gridViewForums.SelectedDataKey["ForumId"]);
            ////int.Parse(Request.QueryString["ForumId"])
            //ForumTemplateDetail objTemplate = new ForumTemplateDetail();
            //objTemplate.AddForumTemplateDetails(int.Parse(dropDownListTemplateDetailsId.SelectedValue), forum.ForumId);
            //dropDownListTemplateDetailsId.DataBind();
            //gridViewForumTemplates.DataBind();
            Session["TemplateDetailsId"] = null;
            gridViewTemplateDetails.DataBind();
            btnAdd.Text = "Add New Template";
            mdlPopup.Show();
        }

        //protected void TemplateDetailsIdLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        //{
        //    Forum forum = GetForumSession((int)Session["ucForum_SelectedValue"]);
        //    ForumTemplateDetail obj = new ForumTemplateDetail();
        //    e.Result = obj.GetRemainingTemplateDetailsOfForum(forum.ForumId, forum.ForumTypeId.Value);
        //}

        protected void gridViewForumTemplates_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblTemplate = (Label)e.Row.FindControl("lblTemplate");
                //LinkButton linkButton = null;
                
                int templateDetailsId = (int)gridViewForumTemplates.DataKeys[e.Row.RowIndex].Values["TemplateDetailsId"];
                LinqDataClassesDataContext dc = new LinqDataClassesDataContext();
                TemplateDetail objTemplateDetails = new TemplateDetail();
                objTemplateDetails = objTemplateDetails.GetTemplateDetails(templateDetailsId);
                lblTemplate.Text = objTemplateDetails.Template.TemplateName;
                
                //linkButton = (LinkButton)e.Row.FindControl("btnChangeTemplateDetailsId");
                //if (linkButton != null)
                //    linkButton.ID += objTemplateDetails.Template.TemplateId.ToString();
                //else
                //    linkButton = (LinkButton)e.Row.FindControl("btnChangeTemplateDetailsId" + objTemplateDetails.Template.TemplateId.ToString());
            }
        }

        protected void ForumTemplateDetailsLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            int id = -1;
            if (Session["ucForum_SelectedValue"] != null)
                id = (int)Session["ucForum_SelectedValue"];

            ForumTemplateDetail obj = new ForumTemplateDetail();
            IQueryable<ForumTemplateDetail> query = obj.GetForumTemplateDetailsByForum(id);
            e.Result = query;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Forum objForum = GetForumSession((int)Session["ucForum_SelectedValue"]);
            int templateDetailsId = -1;
            ForumTemplateDetail objForumTemplateDetail = new ForumTemplateDetail();
            foreach (GridViewRow row in gridViewTemplateDetails.Rows)
            {
                CheckBox chkCheckItem = (CheckBox)row.FindControl("chkCheckItem");
                if (chkCheckItem.Checked)
                {
                    if (Session["TemplateDetailsId"] != null) //Change 
                    {
                        //Remove templateDetailsId of Template contains TemplateId
                        templateDetailsId = int.Parse(Session["TemplateDetailsId"].ToString());
                        TemplateDetail objTemplateDetails = new TemplateDetail();
                        objTemplateDetails.GetTemplateDetails(templateDetailsId);
                        objForumTemplateDetail.Delete(templateDetailsId, objForum.ForumId);

                        //Add selected templateDetailsId
                        templateDetailsId = (int)gridViewTemplateDetails.DataKeys[row.RowIndex]["TemplateDetailsId"];
                        objForumTemplateDetail.Add(templateDetailsId, objForum.ForumId);
                    }
                    else //Add new
                    {
                        templateDetailsId = (int)gridViewTemplateDetails.DataKeys[row.RowIndex]["TemplateDetailsId"];
                        if (!objForumTemplateDetail.IsExist(templateDetailsId, objForum.ForumId))
                            objForumTemplateDetail.Add(templateDetailsId, objForum.ForumId);
                    }
                }
            }
            gridViewForumTemplates.DataBind();
        }

        protected void btnChangeTemplateDetailsId_Click(object sender, EventArgs e)
        {
            
        }

        protected void gridViewForumTemplates_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Change")
            {
                Session["TemplateDetailsId"] = e.CommandArgument;
                gridViewTemplateDetails.DataBind();
                btnAdd.Text = "Change Template";
                mdlPopup.Show();
            }
        }
    }
}